package com.cecsmsserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.entity.DiningMenu;
import com.cecsmsserve.mapper.DiningMenuMapper;
import com.cecsmsserve.service.DiningMenuService;
import com.cecsmsserve.util.result.CommonResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class DiningMenuServiceImpl extends ServiceImpl<DiningMenuMapper, DiningMenu> implements DiningMenuService {
    
    @Override
    public CommonResult list(Integer pageNum, Integer pageSize, String name, String category) {
        try {
            Page<DiningMenu> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<DiningMenu> wrapper = new LambdaQueryWrapper<>();
            
            // 添加查询条件
            if (StringUtils.hasText(name)) {
                wrapper.like(DiningMenu::getName, name);
            }
            if (StringUtils.hasText(category)) {
                wrapper.eq(DiningMenu::getCategory, category);
            }
            
            // 按创建时间降序排序
            wrapper.orderByDesc(DiningMenu::getCreateTime);
            
            return CommonResult.success(page(page, wrapper));
        } catch (Exception e) {
            return CommonResult.failed("获取菜品列表失败：" + e.getMessage());
        }
    }
    
    @Override
    public CommonResult add(DiningMenu menu) {
        try {
            menu.setCreateTime(LocalDateTime.now());
            menu.setUpdateTime(LocalDateTime.now());
            if (save(menu)) {
                return CommonResult.success("添加成功");
            }
            return CommonResult.failed("添加失败");
        } catch (Exception e) {
            return CommonResult.failed("添加失败：" + e.getMessage());
        }
    }
    
    @Override
    public CommonResult update(DiningMenu menu) {
        try {
            menu.setUpdateTime(LocalDateTime.now());
            if (updateById(menu)) {
                return CommonResult.success("更新成功");
            }
            return CommonResult.failed("更新失败");
        } catch (Exception e) {
            return CommonResult.failed("更新失败：" + e.getMessage());
        }
    }
    
    @Override
    public CommonResult delete(Integer id) {
        try {
            if (removeById(id)) {
                return CommonResult.success("删除成功");
            }
            return CommonResult.failed("删除失败");
        } catch (Exception e) {
            return CommonResult.failed("删除失败：" + e.getMessage());
        }
    }
    
    @Override
    public CommonResult updateStatus(Integer id, Integer status) {
        try {
            DiningMenu menu = getById(id);
            if (menu == null) {
                return CommonResult.failed("菜品不存在");
            }
            
            menu.setStatus(status);
            menu.setUpdateTime(LocalDateTime.now());
            
            if (updateById(menu)) {
                return CommonResult.success("状态更新成功");
            }
            return CommonResult.failed("状态更新失败");
        } catch (Exception e) {
            return CommonResult.failed("状态更新失败：" + e.getMessage());
        }
    }
} 